Package de.yaams.maker.helper.language

Source Code of de.yaams.maker.helper.language.T

/**
*
*/
package de.yaams.maker.helper.language;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.commons.lang.LocaleUtils;

import de.yaams.maker.helper.Log;
import de.yaams.maker.helper.Setting;
import de.yaams.maker.helper.extensions.ExtentionManagement;
import de.yaams.maker.helper.extensions.IExtension;
import de.yaams.maker.helper.gui.YSettingHelper;
import de.yaams.maker.helper.gui.form.FormElement;
import de.yaams.maker.helper.gui.form.FormElementChangeListener;
import de.yaams.maker.helper.gui.form.FormInfo;
import de.yaams.maker.helper.gui.form.core.FormBuilder;
import de.yaams.maker.helper.wizard.WizardLanguagePage;
import de.yaams.maker.helper.wizard.WizardManagement;
import de.yaams.maker.programm.YAamsCore;

/**
* @author steven
*
*/
public class T {

  protected static ArrayList<ResourceBundle> bundles;
  protected static ArrayList<String> bundleIdentizer;
  protected static ArrayList<Locale> lang;

  /**
   * Contains the language cache for this language
   */
  protected static HashMap<String, String> cache;

  protected static Locale locale;

  /**
   * Set the actual locale
   *
   * @param l
   */
  public static void setLocale(String locale) {
    setLocale(LocaleUtils.toLocale(locale));
  }

  /**
   * Set the actual locale
   *
   * @param l
   */
  public static void setLocale(Locale locale) {

    // exist?
    if (!lang.contains(locale)) {
      addLang(locale);
    }

    // prepate it
    cache.clear();

    // set it
    T.locale = locale;
    Locale.setDefault(locale);

    // reload all
    bundles.clear();
    for (String s : bundleIdentizer) {
      bundles.add(ResourceBundle.getBundle(s, locale, new UTF8Control()));
    }

  }

  /**
   * Init
   */
  public static void init() {
    // create
    lang = new ArrayList<Locale>();
    cache = new HashMap<String, String>();
    bundles = new ArrayList<ResourceBundle>();
    bundleIdentizer = new ArrayList<String>();

    // setup
    if (YAamsCore.startmode == 1 || !Setting.getSystemPreferences().getBoolean("lang.dontask", false)) {
      setLocale(Locale.getDefault());
      WizardManagement.addPage(new WizardLanguagePage());
    } else {
      setLocale(Setting.getSystemPreferences().get("lang.default", Locale.getDefault().toString()));
    }

    // add lang
    if (!locale.getLanguage().equals(Locale.GERMAN.getLanguage())) {
      addLang(Locale.GERMAN);
    }
    if (!locale.getLanguage().equals(Locale.ENGLISH.getLanguage())) {
      addLang(Locale.ENGLISH);
    }

    // add core
    addRessBundle("de.yaams.maker.helper.language.lang");

  }

  /**
   * Init
   */
  public static void addOptions() {

    // add save
    ExtentionManagement.add(ExtentionManagement.SAVE, new IExtension() {

      @Override
      public void work(HashMap<String, Object> objects) {

        Setting.set("lang.default", locale.toString());

      }
    });

    // add gui

    // add setting
    ExtentionManagement.add("form.options.info", new IExtension() {

      @Override
      public void work(HashMap<String, Object> objects) {
        FormBuilder form = (FormBuilder) objects.get("form");
        form.addElement("basic.lang", new FormInfo(T.r("lang"), locale.getDisplayName()));

      }
    });

    // add setting
    ExtentionManagement.add("form.options.system", new IExtension() {

      @Override
      public void work(HashMap<String, Object> objects) {

        // add it
        FormBuilder form = (FormBuilder) objects.get("form");
        form.addElement("basic.lang",
            YSettingHelper.combo(null, T.r("lang"), "lang", "de", getLangIDs(), getLangNames()).setInfoTxt(T.r("lang.translate"))
                .addChangeListener(new FormElementChangeListener() {

                  @Override
                  public void stateChanged(FormElement form) {
                    setLocale(LocaleUtils.toLocale(form.getContentAsString()));

                  }
                }));
      }
    });
  }

  /**
   * Add a new Ressource Bundle
   *
   * @param identizer
   */
  public static void addRessBundle(String identizer) {
    Log.ger.info("Add Ressource Bundle " + identizer);
    bundleIdentizer.add(0, identizer);
    bundles.add(0, ResourceBundle.getBundle(identizer, new UTF8Control()));
  }

  /**
   * Add a new Lang
   *
   * @param identizer
   */
  public static void addLang(Locale locale) {
    Log.ger.info("Add Lang " + locale);
    lang.add(locale);
  }

  /**
   * Method to get the translated strings
   *
   * @param mess
   * @return
   */
  public static String r(String mess) {
    // in cache?
    if (!cache.containsKey(mess)) {
      for (ResourceBundle r : bundles) {
        try {
          cache.put(mess, r.getString(mess));
          break;
        } catch (Throwable t) {
        }
      }
      // nothing found?
      if (!cache.containsKey(mess)) {
        cache.put(mess, "!" + mess + "!");
      }
    }
    // get it
    return cache.get(mess);
  }

  /**
   * Method to get the translated strings
   *
   * @param mess
   * @param data
   * @return
   */
  public static String r(String mess, Object... data) {
    return MessageFormat.format(r(mess), data);
  }

  /**
   * Helpermethod to build an array over language
   *
   * @return
   */
  public static String[] getLangNames() {

    String[] names = new String[lang.size()];
    // add all
    for (int i = 0, l = lang.size(); i < l; i++) {
      names[i] = lang.get(i).getDisplayName();
    }

    return names;
  }

  /**
   * Helpermethod to build an array over language ids
   *
   * @return
   */
  public static String[] getLangIDs() {

    String[] names = new String[lang.size()];
    // add all
    for (int i = 0, l = lang.size(); i < l; i++) {
      names[i] = lang.get(i).toString();
    }

    return names;
  }

  /**
   * @return the locale
   */
  public static Locale getLocale() {
    return locale;
  }

}
TOP

Related Classes of de.yaams.maker.helper.language.T

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.